<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{number}}.{{title}}</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript" charset="UTF-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" type="text/javascript" charset="UTF-8"></script>
    <!-- 引入jquery CDN -->
     <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <style>
        .back-button {
            display: inline-block;
            margin-bottom: 1.5rem;
            padding: 0.5rem 1rem;
            background-color: #007bff;
            color: #fff;
            border-radius: 0.375rem;
            text-decoration: none;
            transition: background-color 0.3s ease;
        }

        .back-button:hover {
            background-color: #0056b3;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }

        .container {
            display: flex;
            flex: 1;
            overflow: hidden;
        }

        .left-content {
            flex: 1;
            margin-top: 15px;
            margin-left: 15px;
            padding: 20px;
            overflow-y: auto;
        }

        .right-editor {
            flex: 1;
            padding: 20px;
            overflow-y: auto;
        }

        /* 底部输出与提交区域 */
        .bottom-panel {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 1rem 2rem;
            background: #f8f9fa;
            border-top: 1px solid #e9ecef;
            box-shadow: 0 -2px 10px rgba(0,0,0,0.05);
            height: 200px; /* 设置固定高度 */
            overflow-y: auto; /* 添加垂直滚动条 */
        }

        .output-area {
            flex: 1;
            margin-right: 1rem;
            padding: 0.5rem;
            border: 1px solid #dee2e6;
            border-radius: 0.375rem;
            white-space: pre-wrap;
            height: 100%; /* 填充底部面板高度 */
            overflow-y: auto; /* 添加垂直滚动条 */
        }

        .submit-button {
            padding: 0.75rem 1.5rem;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 0.375rem;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .submit-button:hover {
            background-color: #218838;
        }

        div.ace_editor {
            height: 100%;
            width: 100%;
        }
        .bottom-panel .output-area {
            margin-top: 15px;
            margin-left: 15px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="left-content">
            <a href="../template_html/all_questions.html" class="back-button">
                <i class="fas fa-arrow-left"></i> 返回题目列表
            </a>
            <h3><span id="number">{{number}}</span>.{{title}}.{{star}}</h3>
            <pre>{{desc}}</pre>
        </div>
        <div class="right-editor">
            <div id="code" class="ace_editor"><textarea class="ace_text-input">{{pre_code}}</textarea></div>
        </div>
    </div>

    <!-- 底部输出与提交按钮区域 -->
    <div class="bottom-panel">
        <div class="output-area"></div>
        <button class="submit-button" onclick="submitCode()">提交代码</button>
    </div>

    <script>
        const editor = ace.edit("code");
        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/c_cpp");
        editor.setOptions({
            fontSize: "16px",
            showPrintMargin: false,
            highlightActiveLine: true,
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });

        // 模拟提交代码功能（实际需后端支持）
        function submitCode() {
            var code = editor.getSession().getValue();
            //console.log(code);
            var number = $(".container .left-content h3 #number").text();
            var judge_url = "/judge/" + number;
            //console.log(judge_url);
            // 这里可以添加实际代码提交逻辑（如通过 AJAX 发送到后端）
            $.ajax({
                method: 'Post',
                url: judge_url,
                dataType: 'json',
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    'code':code,
                    'input': ''
                }),
                success: function(data){
                    //成功得到结果
                    //console.log(data);
                    show_result(data);
                }
            })
            // 模拟输出结果
            function show_result(data)
            {
                //console.log(data.status);
                //console.log(data.reason);
                var result_div = $(".bottom-panel .output-area");
                result_div.empty();

                var _status = data.status;
                var _reason = data.reason;

                var reason_lable = $( "<p>",{
                        text: _reason
                });
                reason_lable.appendTo(result_div);
                if(status == 0){
                    //请求成功，编译运行过程没问题。但是结果是否通过看测试用例的结果
                    var _stdout = data.stdout;
                    var _stderr = data.stderr;

                    var stdout_lable = $("<pre>", {
                        text: _stdout
                    });

                    var stderr_lable = $("<pre>", {
                        text: _stderr
                    })

                    stdout_lable.appendTo(result_div);
                    stderr_lable.appendTo(result_div);
                }
                else{
                    //编译运行出错
                }
            }
        }
    </script>
</body>

</html>